<template>
  <div class="register">
    <van-uploader :after-read="afterRead" />
    <img :src="imgurl" alt="" v-if="imgurl" width="200px" height="200px" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="userName"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { reqRegister } from "../../api/user";
import { Toast } from "vant";
export default {
  data() {
    return {
      userName: "",
      password: "",
      imgurl: "",
    };
  },
  methods: {
    async onSubmit(values) {
      console.log("submit", values);
      const result = await reqRegister({
        userName: this.userName,
        password: this.password,
        imgurl: this.imgurl,
      });
      console.log(result);
      if (result.code == "success") {
        this.$router.push("./login");
      } else {
        Toast(result.message);
      }
    },
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      console.log(file);
      this.imgurl = file.content;
    },
  },
};
</script>

<style scoped></style>
